<?php
/**
 * Created by PhpStorm.
 * User: xi
 * Date: 2020-05-02
 * Time: 16:54
 */

namespace app\api\service;


use app\admin\model\Cmdlog;
use app\admin\model\Roomclean;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproom;
use app\admin\model\Shoproombook;
use app\admin\model\Shopstaff;
use app\admin\model\Shoptable;
use app\admin\model\Userorderprofit;
use app\admin\model\Usertuan;
use app\admin\model\Userwithdraw;

class TableService
{

    public $openCmdUrl = "http://47.97.67.38:8080/cmd/open";

    //public $tqV3CmdUrl = "http://47.97.67.38:8881/qx/openTable";

    public $openTqCmdUrl = "http://47.97.67.38:8080/cmd/testTq";


    public $tqV3CmdUrl = "http://112.74.78.56:8881/qx/openTable";//从雀和友复制


    public function close($order, $device)
    {

        $req = array();
        if ($device['version'] == 1) {
            $req = array();
            $req['switchOne'] = 0;
            $req['switchTwo'] = 0;
            $req['door'] = 2;
            $req['hello'] = "欢迎下次来玩";
            $req['orderNo'] = $order['order_no'];
            $req['sn'] = $device['sn'];
            $ret = http_post_json($this->openTqCmdUrl, json_encode($req));
            $ret = json_decode($ret, true);
            if ($ret['code'] == 0) {
                $msg = "正在打开中";
            }
        } else if ($device['version'] == 2) {
            $req = array();
            $req['sec'] = 0;
            $req['voice'] = "欢迎下次来玩";
            $req['orderNo'] = $order['order_no'];
            $req['sn'] = $device['sn'];

            $ret = http_post_json($this->openCmdUrl, json_encode($req));
            $ret = json_decode($ret, true);
            if ($ret['code'] == 0) {
                $msg = "正在打开中";
            }

        } else if ($device['version'] == 3) {
            $req = array();
            $req['sec'] = 0;
            $req['orderId'] = $order['id'];
            $req['deviceSn'] = $device['device_sn'];

            $ret = http_post_json($this->tqV3CmdUrl, json_encode($req));
            $ret = json_decode($ret, true);
            if ($ret['code'] == 0) {
                $msg = "正在打开中";
            }
        }

        $cmdModel = new Cmdlog();
        $cmdModel->insert(['user_id' => $order['user_id'], 'order_no' => $order['order_no'], 'cmd' => json_encode($req), 'create_time' => time(), 'shop_id' => $order['shop_id'], 'room_id' => $order['table_id']]);

    }

    public function open($order, $device)
    {

        if ($order['status'] != 'doing') {


            error_log("====订单号开台不正常=====" . json_encode($order));
            return;
        }
        $req = array();

        if ($device['version'] == 1) {
            $req = array();
            $req['switchOne'] = 1;
            $req['switchTwo'] = 1;
            $req['door'] = 1;
            $req['hello'] = "您好开台成功祝您玩得开心";
            $req['orderNo'] = $order['order_no'];
            $req['sn'] = $device['sn'];
            $ret = http_post_json($this->openTqCmdUrl, json_encode($req));
            $ret = json_decode($ret, true);
            if ($ret['code'] == 0) {
                $msg = "正在打开中";
            }
        } else if ($device['version'] == 2) {
            $req = array();
            $req['sec'] = $order['min'] * 60;
            $req['voice'] = "您好开台成功,祝您玩得开心";
            $req['orderNo'] = $order['order_no'];
            $req['sn'] = $device['sn'];

            $ret = http_post_json($this->openCmdUrl, json_encode($req));
            $ret = json_decode($ret, true);
            if ($ret['code'] == 0) {
                $msg = "正在打开中";
            }


        } else if ($device['version'] == 3) {
            $req = array();
            $req['sec'] = $order['min'] * 60;
            $req['orderId'] = $order['id'];
            $req['deviceSn'] = $device['device_sn'];

            $ret = http_post_json($this->tqV3CmdUrl, json_encode($req));
            $ret = json_decode($ret, true);
            if ($ret['code'] == 0) {
                $msg = "正在打开中";
            }
        }


        $cmdModel = new Cmdlog();
        $cmdModel->insert(['user_id' => $order['user_id'], 'order_no' => $order['order_no'], 'cmd' => json_encode($req), 'create_time' => time(), 'shop_id' => $order['shop_id'], 'room_id' => $order['table_id']]);
        
    }

    public function cacFee($book_end, $book_start, $rule)
    {
        $minute = floor(($book_end - $book_start) % 86400 / 60);
        $hour = ceil($minute / 60);
        $start = date("H", $book_start);


        $time1 = explode("~", $rule['time1']);
        if (count($time1) == 1) {
            $startTime1 = explode("-", $rule['time1'])[0];
            $endTime1 = explode("-", $rule['time1'])[1];
        } else {
            $startTime1 = explode("~", $rule['time1'])[0];
            $endTime1 = explode("~", $rule['time1'])[1];
        }
        $time2 = explode("~", $rule['time2']);
        if (count($time2) == 1) {
            $startTime2 = explode("-", $rule['time2'])[0];
            $endTime2 = explode("-", $rule['time2'])[1];
        } else {
            $startTime2 = explode("~", $rule['time2'])[0];
            $endTime2 = explode("~", $rule['time2'])[1];
        }
        $startTime1 = intval($startTime1);
        $endTime1 = intval($endTime1);


        $startTime2 = intval($startTime2);
        $endTime2 = intval($endTime2);
        $slots = array();

        if ($endTime1 < $startTime1) {
            $obj = array();
            $obj['begin'] = $startTime1;
            $obj['end'] = 24;
            $obj['charge'] = $rule['price1'];
            $slots[] = $obj;
            $obj = array();
            $obj['begin'] = 0;
            $obj['end'] = $endTime1;
            $obj['charge'] = $rule['price1'];
            $slots[] = $obj;

        } else {
            $obj = array();
            $obj['begin'] = $startTime1;
            $obj['end'] = $endTime1;
            $obj['charge'] = $rule['price1'];
            $slots[] = $obj;
        }

        if ($endTime2 < $startTime2) {
            $obj = array();
            $obj['begin'] = $startTime2;
            $obj['end'] = 24;
            $obj['charge'] = $rule['price2'];
            $slots[] = $obj;
            $obj = array();
            $obj['begin'] = 0;
            $obj['end'] = $endTime2;
            $obj['charge'] = $rule['price2'];
            $slots[] = $obj;

        } else {
            $obj = array();
            $obj['begin'] = $startTime2;
            $obj['end'] = $endTime2;
            $obj['charge'] = $rule['price2'];
            $slots[] = $obj;
        }

        $hour = floor($minute / 60);
        $amount = get_charge($start, $hour, $slots, $minute);

        return $amount;
    }

    public function updateTableFree($redis, $shop_id)
    {

        $room_key = "table_count_" . $shop_id;
        $shopTableModel = new Shoptable();
        $roomValue = $shopTableModel->where('shop_id', $shop_id)->where('status', 'free')->count('id');
        $redis->set($room_key, $roomValue);

    }

}